package com.sjy.imagepicker.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;
import com.sjy.imagepicker.R;
import com.sjy.imagepicker.bean.FolderBean;

import java.io.File;
import java.util.List;

/**
 * Created by zhang on 2016/6/29.
 */
    public class PicDirsWindow extends PopupWindow {
    private final LayoutInflater inflater;
    private final Context context;
    private View mConvertView;
    private List<FolderBean> mDatas;
    private ListView mDirLV;
    private DirAdapter adapter;

    public PicDirsWindow(Context context,  List<FolderBean> mDatas) {

        //calWidthAndHeight(context);
        this.context=context;
        this.mDatas = mDatas;
        inflater = LayoutInflater.from(context);
        mConvertView= inflater.inflate(R.layout.popwindow,null);
        mConvertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        setContentView(mConvertView);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);


        setFocusable(true);
        setTouchable(true);

        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable());

        setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction()==MotionEvent.ACTION_OUTSIDE){
                    dismiss();
                    return true;
                }
                return false;
            }
        });

        initView();
        initEvent();
    }

    private void initView() {
        mDirLV = (ListView) mConvertView.findViewById(R.id.pop_dir_lv);
        adapter = new DirAdapter(context,mDatas);
        mDirLV.setAdapter(adapter);
    }

    private void initEvent() {
        mDirLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                FolderBean bean=adapter.getItem(position);

                if (onDirSelectedListener != null) {
                    onDirSelectedListener.onDirSelected(bean);
                }
            }
        });
    }

    /*private void calWidthAndHeight(Context context) {
        WindowManager wm= (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics=new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);

        mWidth=outMetrics.widthPixels;
        mHeight= (int) (outMetrics.heightPixels*0.7);
    }*/

    private class DirAdapter extends ArrayAdapter<FolderBean>{
        private final Context context;
        private List<FolderBean> mDatas;

        public DirAdapter(Context context, List<FolderBean> mDatas) {
            super(context, 0, mDatas);
            this.context=context;
            this.mDatas=mDatas;
        }

        @Override
        public int getCount() {
            return this.mDatas.size();
        }

        @Override
        public FolderBean getItem(int position) {
            return this.mDatas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder=null;
            if (convertView == null) {
                convertView=inflater.inflate(R.layout.pop_lv_item,parent,false);
                viewHolder=new ViewHolder();
                viewHolder.mDirImg= (ImageView) convertView.findViewById(R.id.pp_dir_lv);
                viewHolder.mDirName= (TextView) convertView.findViewById(R.id.pp_dir_name);
                viewHolder.mDirCount= (TextView) convertView.findViewById(R.id.pp_dir_count);
                convertView.setTag(viewHolder);
            }
            else{
                viewHolder= (ViewHolder) convertView.getTag();
            }

            FolderBean bean=getItem(position);

            RequestOptions options = new RequestOptions()
                    .placeholder(R.drawable.ic_no)
                    .error(R.drawable.ic_no)
                    .centerCrop()
                    .priority(Priority.HIGH);


            Glide.with(context)
                    .load(new File(bean.getFirstImgPath()))
                    .apply(options)
                    .into(viewHolder.mDirImg);

            viewHolder.mDirName.setText(bean.getName());
            viewHolder.mDirCount.setText(bean.getCount()+"张");
            return convertView;
        }
    }

    private static  class ViewHolder{
        ImageView mDirImg;
        TextView mDirName;
        TextView mDirCount;
    }

    public interface OnDirSelectedListener{
        void onDirSelected(FolderBean bean);
    }

    private OnDirSelectedListener onDirSelectedListener;

    public void setOnDirSelectedListener(OnDirSelectedListener onDirSelectedListener) {
        this.onDirSelectedListener = onDirSelectedListener;
    }
}
